import 'dart:io';

import 'package:flutter_base/common/utils/utils.dart';
import 'package:flutter/services.dart';
import 'package:jpush_flutter/jpush_flutter.dart';

class ATPush{
  static void iOSPush(Map<String, dynamic> message){
    try{
      Map<String, dynamic> jsonString =  new Map<String, dynamic>.from(message);
      print(jsonString);
      Map<String, dynamic> aps =  new Map<String, dynamic>.from(jsonString["aps"]);
      print(aps);
      Map<String, dynamic> alert =  new Map<String, dynamic>.from(aps["alert"]);
      print(alert);
      print(alert["title"]);

      // Alert info = Alert.fromJson(alert);
      // print(info.title);
      // print(info.body);
      // ATAlertView.showAlertView(title:info.title ,message:info.body,hights: "取消",normals: "确定",completion: (index,title){
      //   if(index == 1){
      //     ATJump.jumpToHelp();
      //   }
      // });
    }catch (error){
      print("error json = $error");
    }
  }
  static Future<void> initPlatformState() async {
    String platformVersion;
    final JPush jpush = new JPush();
    try {
      jpush.addEventHandler(
          onReceiveNotification: (Map<String, dynamic> message) async {
            print("接收到推送: $message");
            //极光发送通知
            if(Platform.isAndroid){
              // ATAlertView.showAlertView(title:message["title"] ,message: message["alert"],hights: "取消",normals: "确定",completion: (index,title){
              //   if(index == 1){
              //     ATJump.jumpToHelp();
              //   }
              // });
            }else{
              // iOSPush(message);
              Map<String, dynamic> jsonString =  new Map<String, dynamic>.from(message);
              print('jsonString= $jsonString');
              Map<String, dynamic> aps =  new Map<String, dynamic>.from(jsonString["aps"]);
              print('aps =$aps');
              int badge =  aps["badge"];
              print('badge========>>>>>>$badge');
              int currentNumber =StorageUtil().getInt('badge');
              if(badge>currentNumber){
                StorageUtil().setInt('badge', badge);
              }
            }

          }, onOpenNotification: (Map<String, dynamic> message) async {
        print("通过点击推送进入app: $message");
        // jpush.setBadge(0);

        // jpush.clearNotification();
        if(Platform.isIOS){
          // iOSPush(message);
          Map<String, dynamic> jsonString =  new Map<String, dynamic>.from(message);
          print('jsonString= $jsonString');
          Map<String, dynamic> aps =  new Map<String, dynamic>.from(jsonString["aps"]);
          print('aps =$aps');
          int badge =  aps["badge"];
          print('badge========>>>>>>$badge');
          // badge  = badge-1;
          int currentNumber =StorageUtil().getInt('badge');
          if (currentNumber > 0) {
            currentNumber--;

          }
          print('currentNumber = $currentNumber');
          StorageUtil().setInt('badge', currentNumber);
          jpush.setBadge(currentNumber);
        }
      }, onReceiveMessage: (Map<String, dynamic> message) async {
        print("接收到自定义消息: $message");
        //极光自定义消息
        if (Platform.isIOS){
          // iOSPush(message);

        }else if(Platform.isAndroid){
          // ATAlertView.showAlertView(title: "消息推送",message: message["message"],hights: "取消",normals: "确定",completion: (index,title){
          //   if(index == 1){
          //     ATJump.jumpToHelp();
          //   }
          // });
        }

      }, onReceiveNotificationAuthorization:
          (Map<String, dynamic> message) async {
            print("通知权限状态: $message");

      });
    } on PlatformException{
      platformVersion = 'Failed to get platform version.';
    }
    jpush.setup(
      appKey: '9d30b962f70602b08dbe564e', //你自己应用的 AppKey
      channel: "developer-default",
      production: false,
      debug: true,
    );
    jpush.applyPushAuthority(
        new NotificationSettingsIOS(sound: true, alert: true, badge: true));
    jpush.getRegistrationID().then((rid) {
      print("flutter get registration id : $rid");
    });
  }
}
//注意这里的appKey需要到极光的官网申请